package com.txw.javaCore.server.controller;

import com.txw.javaCore.api.response.BaseResponse;
import com.txw.javaCore.api.response.StatusCode;
import com.txw.javaCore.model.request.DelayJobRequest;
import com.txw.javaCore.server.service.MqDelayService;
import com.txw.javaCore.server.util.ValidatorUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @PROJECT_NAME: JavaCoreB2
 * @DESCRIPTION:
 * @Author: 涂玄武
 * @DATE: 2021/1/5 14:09
 */
@RestController
@RequestMapping(value = "mq/delay")
public class MqDelayController {

    @Autowired
    private MqDelayService mqDelayService;

    /**
     * 处理TTL取值灵活性很高的任务(如发送邮件) - rabbitmq 死信队列
     * @param request
     * @param result
     * @return
     */
    @RequestMapping(value = "mail/dead")
    public BaseResponse manageDeadJob(@RequestBody @Validated DelayJobRequest request, BindingResult result){
        String checkRes = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(checkRes)){
            return new BaseResponse(StatusCode.InvalidParams.getCode(),checkRes);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            mqDelayService.manageDeadJob(request);
        }catch (Exception e){
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    @RequestMapping(value = "mail/delay")
    public BaseResponse manageDelay(@RequestBody @Validated DelayJobRequest request,BindingResult result){
        String checkRes = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(checkRes)){
            return new BaseResponse(StatusCode.InvalidParams.getCode(),checkRes);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            mqDelayService.manageDelayJob(request);
        }catch (Exception e){
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }
}
